#include <cpuset.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#ifdef DEBUG
void show_all()
{
		fprintf(stderr,"pid:%d\tdestcpu:%d\tforceflag:%d\tcreateflag:%d\tlistflag:%d\tdelflag:%d\n",
						pid, destcpu, forceflag & optflag, createflag, listflag, delflag);
		fprintf(stderr,"optname:%s\tnewname:%s\tset_cpulist:%s\t"
						"cpulist:%s\n",optname, newname, set_cpulist, cpulist);
}
#else
void show_all(){}
#endif

#ifdef DEBUG
/* check errno */
void check_errno(const int line, const char *name, pid_t pid)
{
	switch(errno)
	{
		case 0:	// no error
			break;
		case EPERM:
			fprintf(stderr, "Operation not permitted.\n");
			break;
		case ENODEV:
			fprintf(stderr, "no cpuset mounted!LINE:%d\n",line);
			break;
		case ENOENT:
			fprintf(stderr, "cpuset:no such cpuset: %s LINE:%d\n", name,line);
			break;
		case ESRCH:
			fprintf(stderr, "cpuset:no such process: %d LINE:%d\n",pid,line);
			break;
		case EACCES:
			fprintf(stderr, "cpuset:permition denied:LINE: %d\n", line);
			break;
		case EEXIST:
			fprintf(stderr, "cpuset:file exist  LINE: %d\n", line);
			break;
		case EINVAL:
			fprintf(stderr, "cpuset:invalid argument: %s LINE:%d\n", name, line);
			break;
		case EBUSY:
			fprintf(stderr, "cpuset:not empty: %s LINE:%d\n", name, line);
			break;
		case ENOSPC:
			fprintf(stderr, "cpuset:no memory on cpuset:%s LINE:%d\n", name, line);
			break;
		default:
			fprintf(stderr, "error happen at LINE: %d: %s %d\n", line, "unknow errno:",errno);
	}
	exit(errno);	// exit on error
}
#else
void check_errno(const int line, const char *name, pid_t pid)
{
	switch(errno)
	{
		case 0:	// no error
			break;
		case EPERM:
			fprintf(stderr, "cpuset: Operation not permitted.\n");
			break;
		case EBOUND:
			fprintf(stderr, "cpuset: process were bound by cpu, can't be moved!\n");
			break;
		case ENOCPU:
			fprintf(stderr, "cpuset: cpulist error!\n");
			break;
		case ENODEV:
			fprintf(stderr, "no cpuset mounted!\n");
			break;
		case ENOENT:
			fprintf(stderr, "cpuset: no such cpuset: \"%s\"\n", name);
			break;
		case ESRCH:
			fprintf(stderr, "cpuset: no such process: %d\n",pid);
			break;
		case EACCES:
			fprintf(stderr, "cpuset: permition denied.\n");
			break;
		case EEXIST:
			fprintf(stderr, "cpuset: cpuset \"%s\" exist.\n", name);
			break;
		case EINVAL:
			fprintf(stderr, "cpuset: invalid argument: %s\n", name);
			break;
		case EBUSY:
			fprintf(stderr, "cpuset: not empty: %s\n", name);
			break;
		case ENOSPC:
			fprintf(stderr, "cpuset: no memory on cpuset:%s\n", name);
			break;
		default:
			fprintf(stderr, "error happen: unknow errno:%d\n",errno);
	}
	exit(EINVAL);	// exit on error
}
#endif			// debug

void ck_error(int status, int errnum, char *fmt, ...)
{
	va_list args;
	char *strerr;

	if (errnum)
	{
		va_start(args, fmt);
		vfprintf(stderr, fmt, args);
		va_end(args);
		if (!status)	// user defined message
			goto out;
		switch(errnum)
		{
			case ENOCPU:
					strerr = "Cpu/Cpulist error";
					break;
			case EBOUND:
					strerr = "Process bound by kernel";
					break;
			case ENODEV:
					strerr = "No cpuset mount";
					break;
			case ENOSYS:
					strerr = "Please use K2OS";
					break;
			case ENOENT:
					strerr = "NO such target";
					break;
			case EEXIST:
					strerr = "Target exist";
					break;
			default:
					strerr = strerror(errnum);
					break;
		}
		fprintf(stderr, "%s\n", strerr);
	}
out:
	exit(errnum);
}
